/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

/**
 * 栈, 先进后出
 */
export class Stack<T> {
  private readonly stack: T[] = [];
  constructor() {
    this.stack = [];
  }

  push(element: T) {
    return this.stack.push(element);
  }

  pop(): T | undefined {
    if (this.isEmpty()) {
      return undefined;
    } else {
      return this.stack.pop();
    }
  }

  peek(): T | undefined {
    if (!this.isEmpty()) {
      return this.stack[this.size() - 1];
    } else {
      return undefined;
    }
  }

  size() {
    return this.stack.length;
  }

  isEmpty() {
    return this.stack.length === 0;
  }
}
